<template>
  <div class="recommendModuleManagement">
    <h3>推荐模块管理</h3>
    <div class="search">
      <div class="cond1 cond">
        <el-input placeholder="请输入模块ID" v-model="search1">
          <template slot="prepend">模块ID</template>
        </el-input>
      </div>
      <div class="cond2 cond">
        <label>模块类型</label>
        <el-select v-model="search2" placeholder="请选择">
          <el-option
            v-for="item in optionsModuleType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="cond1 cond">
        <el-input placeholder="请输入模块标题" v-model="search3">
          <template slot="prepend">模块标题</template>
        </el-input>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-search" @click="searchMethod">搜索</el-button>
      </div>
    </div>
    <div class="opration">
      <el-button type="success" icon="el-icon-plus" @click="addData" v-if="permissions_obj.id_157||permissions_obj.id_all">添加</el-button>
      <!-- <el-button type="danger" icon="el-icon-delete" @click="batchDelete">批量删除</el-button> -->
    </div>
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column type="index" width="50" header-align="center" align="center" label="序号"></el-table-column>
        <el-table-column header-align="center" align="center" prop="id" label="模块ID" width="90"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="module"
          label="模块类型"
          width="100"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="title" label="模块标题"></el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          prop="created_at"
          label="添加时间"
          width="180"
        ></el-table-column>
        <el-table-column header-align="center" align="center" prop="video_count" label="添加视频数量"></el-table-column>
        <el-table-column header-align="center" align="center" prop="limit" label="内容显示数量"></el-table-column>
        <el-table-column header-align="center" align="center" prop="sort" label="排序"></el-table-column>
        <el-table-column header-align="center" align="center" label="开关" width="80">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              @change="changeSwitch(scope.$index, scope.row)"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="操作" width="250" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row)" v-if="permissions_obj.id_158||permissions_obj.id_all">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)" v-if="permissions_obj.id_159||permissions_obj.id_all">删除</el-button>
            <el-button
              size="mini"
              type="success"
              @click="handleToVideo(scope.$index, scope.row)"
              v-if="scope.row.module!='广告'"
            >管理视频</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage1"
        :page-sizes="[10,30,50,100]"
        :page-size="pageSize"
        layout="total, prev, pager, next,sizes,jumper"
        :total="totalSize"
      ></el-pagination>
    </div>
    <!-- //修改广告信息弹窗 -->
    <el-dialog title="广告详情" :visible.sync="modifyEditorDialogVisible" width="40%" center>
      <div class="row">
        <span>模块ID：</span>
        <el-input v-model="inputModuleId" placeholder="请输入广告标题" disabled></el-input>
      </div>
      <div class="row">
        <span>模块类型：</span>
        <el-input v-model="inputModuleType" disabled></el-input>
      </div>
      <div class="row">
        <span>模块名称：</span>
        <el-input v-model="inputModuleName" placeholder="请输入广告标题" clearable></el-input>
      </div>
      <div class="row">
        <span>内容显示数量：</span>
        <el-input v-model="inputLimit" placeholder="请输入显示数量" clearable type="number"></el-input>
      </div>
      <div class="row">
        <span>模块排序：</span>
        <el-input-number v-model="inputModuleSort" :min="1"></el-input-number>
      </div>
      <div class="row">
        <span>状态：</span>
        <el-select v-model="inputStatus" placeholder="请选择">
          <el-option
            v-for="item in optionsStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="dialogBtn">
        <el-button type="primary" size="small" @click="submitModifyEditor">提交</el-button>
        <el-button type="info" size="small" @click="cancelModifyEditor">取消</el-button>
      </div>
    </el-dialog>
    <!-- //添加广告信息弹窗 -->
    <el-dialog title="添加广告" :visible.sync="addAdDialogVisible" width="40%" center>
      <div class="row">
        <span>模块类型：</span>
        <el-select v-model="inputModuleType" placeholder="请选择">
          <el-option
            v-for="item in optionsModuleType2"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="row">
        <span>模块名称：</span>
        <el-input v-model="inputModuleName" placeholder="请输入模块名称" clearable></el-input>
      </div>
      <div class="row">
        <span>内容显示数量：</span>
        <el-input v-model="inputLimit" placeholder="请输入显示数量" clearable type="number"></el-input>
      </div>
      <div class="row">
        <span>模块排序：</span>
        <el-input-number v-model="inputModuleSort" :min="1"></el-input-number>
      </div>
      <div class="row">
        <span>状态：</span>
        <el-select v-model="inputStatus" placeholder="请选择">
          <el-option
            v-for="item in optionsStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="dialogBtn">
        <el-button type="primary" size="small" @click="submitAdd">提交</el-button>
        <el-button type="info" size="small" @click="cancelAdd">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "recommendModuleManagement",
  data() {
    return {
      search1: "",
      search2: "",
      search3: "",
      //模块类型
      optionsModuleType: [
        {
          value: "",
          label: "全部"
        }
      ],
      //弹窗模块类型
      optionsModuleType2: [],
      optionsStatus: [
        {
          value: 0,
          label: "关闭"
        },
        {
          value: 1,
          label: "开启"
        }
      ],
      tableData: [],
      currentPage1: 1,
      pageSize: 10,
      totalSize: 0,
      modifyEditorDialogVisible: false,
      addAdDialogVisible: false,
      //弹窗
      inputModuleId: "",
      inputModuleType: "",
      inputModuleName: "",
      inputLimit: "",
      inputModuleSort: "",
      inputStatus: "",
      // 权限管理
      permissions: [],
      permissions_obj: {}
    };
  },
  created() {
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff = false;
    this.permissions.forEach((item, index) => {
      if (item == -1) {
        oneOff = true;
      } else {
        this.permissions_obj["id_" + item] = item;
      }
    });
    if (oneOff) {
      this.permissions_obj.id_all = true;
    } else {
      this.permissions_obj.id_all = false;
    }
  },
  mounted() {
    this.getModuleType();
    this.getInitData();
  },
  methods: {
    // 分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage1 = val;
      this.getInitData();
    },
    handleSizeChange(val){
      this.pageSize=val;
      this.getInitData();
    },
    searchMethod() {
      this.currentPage1 = 1;
      this.getInitData();
    },
    //添加
    addData() {
      this.inputModuleId = "";
      this.inputModuleType = "";
      this.inputModuleName = "";
      this.inputLimit = "";
      this.inputModuleSort = "";
      this.inputStatus = "";
      this.addAdDialogVisible = true;
    },
    //状态开关监听
    changeSwitch(index, row) {
      let id = row.id;
      let status = row.status;
      if (row.status == 1) {
        row.status = 0;
      } else {
        row.status = 1;
      }
      this.$confirm("是否继续该操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.changeStatus(id, status);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消该操作"
          });
        });
    },
    //改变状态开关接口
    async changeStatus(id, status) {
      let parames = {
        handshake: "",
        data: {
          id: id,
          status: status
        }
      };
      let res = await this.$axios.post(
        this.$api.moduleUpdate,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.getInitData();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //编辑
    handleEdit(index, row) {
      this.inputModuleId = row.id;
      this.inputModuleType = row.module;
      this.inputModuleName = row.title;
      this.inputLimit = row.limit;
      this.inputModuleSort = row.sort;
      this.inputStatus = row.status;
      this.modifyEditorDialogVisible = true;
    },
    //删除
    handleDelete(index, row) {
      this.$confirm("是否继续该操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteMethod(row.id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //删除接口
    async deleteMethod(id) {
      let parames = {
        handshake: "",
        data: {
          id: id
        }
      };
      let res = await this.$axios.post(
        this.$api.moduleDelete,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.getInitData();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //提交修改
    async submitModifyEditor() {
      let parames = {
        handshake: "",
        data: {
          id: this.inputModuleId,
          title: this.inputModuleName,
          limit: this.inputLimit,
          sort: this.inputModuleSort,
          status: this.inputStatus
        }
      };
      let res = await this.$axios.post(
        this.$api.moduleUpdate,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.modifyEditorDialogVisible = false;
        this.getInitData();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //取消提交
    cancelModifyEditor() {
      this.modifyEditorDialogVisible = false;
    },

    //获取初始化列表数据
    async getInitData() {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage1,
          page_size: this.pageSize,
          search: {
            id: this.search1,
            module: this.search2,
            title: this.search3
          }
        }
      };
      let res = await this.$axios.post(
        this.$api.moduleList,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        this.totalSize = this.initData.total;
        this.tableData = this.initData.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },

    //提交添加
    async submitAdd() {
      if (!this.inputModuleType) {
        this.$message({
          message: "请选择模块类型",
          type: "warning"
        });
        return;
      }
      if (!this.inputModuleName) {
        this.$message({
          message: "请填写模块名称",
          type: "warning"
        });
        return;
      }
      if (!this.inputLimit) {
        this.$message({
          message: "请填写内容显示数量",
          type: "warning"
        });
        return;
      }

      let parames = {
        handshake: "",
        data: {
          module: this.inputModuleType,
          title: this.inputModuleName,
          limit: this.inputLimit,
          sort: this.inputModuleSort,
          status: this.inputStatus
        }
      };
      let res = await this.$axios.post(
        this.$api.moduleAdd,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.addAdDialogVisible = false;
        this.getInitData();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    cancelAdd() {
      this.addAdDialogVisible = false;
    },
    //获取模块类型
    async getModuleType() {
      let parames = {
        handshake: "",
        data: {}
      };
      let res = await this.$axios.post(
        this.$api.moduleEnum,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        let data = res.data;
        Object.keys(data).forEach(key => {
          let obj = {};
          obj.label = data[key];
          obj.value = key;
          this.optionsModuleType.push(obj);
          this.optionsModuleType2.push(obj);
        });
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    handleToVideo(index,row){
       if (!this.permissions_obj.id_160 && !this.permissions_obj.id_all) {
        this.$message({
          type: "info",
          message: "没有权限进行该操作"
        });
        return;
      }
      this.$router.push({path:'/moduleVideoManagement',query:{title:row.title,module:row.module,id:row.id}});
    }
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
.el-input {
  width: 2.5rem;
}
label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.search .el-select >>> .el-input__inner {
  border-left: none;
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}
.search .el-select {
  width: 1.67rem;
}
.opration {
  margin-top: 0.2rem;
  padding-left: 0.2rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
.dialogBtn {
  display: flex;
  justify-content: center;
  margin-top: 0.3rem;
}
.row {
  margin-bottom: 0.1rem;
  display: flex;
  align-items: center;
}
.row span {
  width: 1rem;
  display: block;
}
</style>